package com.example.list;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddViewActivity extends AppCompatActivity {
    private NotificationManager mNManager;
    private Notification notify1;
    @SuppressLint("ResourceType")
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_view);


//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//            // 通知渠道的id 这个地方只要一直即可
//            String id = "chat";
//            // 用户可以看到的通知渠道的名字.
//            CharSequence name = "notification channel";
//            // 用户可以看到的通知渠道的描述
//            String description = "notification description";
//            int importance = NotificationManager.IMPORTANCE_HIGH;
//            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
//            // 配置通知渠道的属性
//            mChannel.setDescription(description);
//            // 设置通知出现时的闪灯（如果 android 设备支持的话）
//            mChannel.enableLights(true);
//            mChannel.setLightColor(Color.RED);
//            // 自定义声音
////            mChannel.setSound(Uri.parse("android.resource://" + getPackageName() + "/raw/qqqq"),null);
//            // 设置通知出现时的震动（如果 android 设备支持的话）
//            mChannel.enableVibration(true);
//            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//            //最后在notificationmanager中创建该通知渠道
//            mNotificationManager.createNotificationChannel(mChannel);
//        }
//
//        Bitmap LargeBitmap  = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
//
//        //定义一个PendingIntent点击Notification后启动一个Activity
//        Intent it = new Intent(this, MainActivity.class);
//        PendingIntent pit = PendingIntent.getActivity(this, 0, it, 0);
//
//        notify1 = new Notification.Builder(this,"chat")
//                .setContentTitle("叶良辰")                        //标题
//                .setContentText("我有一百种方法让你呆不下去~")      //内容
//                .setSmallIcon(R.mipmap.ic_launcher)            //设置小图标
//                .setLargeIcon(LargeBitmap)                     //设置大图标
//                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)    //设置默认的三色灯与振动器
//                .setAutoCancel(true)
//                .setContentIntent(pit)
//                .build();
//        //设置点击后取消Notification
//        mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        mNManager.notify(1, notify1);



//        布局
        View view2 = getLayoutInflater().inflate(R.layout.layout_layer_guide_shengjia, null);

        LinearLayout viewById = (LinearLayout) findViewById(R.id.ll_addview);

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.tv_addview);

        //创建一个相对布局relative  包裹父控件
        RelativeLayout relative = new RelativeLayout(this);
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        relative.setLayoutParams(lp);
        relative.setBackgroundColor(Color.GREEN);//透明
// 将Button1 加入到RelativeLayout 中
        Button btn_r1 = new Button(this);
        btn_r1.setText("取消");//设置显示的字符
        btn_r1.setId(24);
        relative.addView(btn_r1);
// 将Button2 加入到RelativeLayout 中
//        Button btn_r2 = new Button(this);
////        btn_r2.setText("确定");//设置显示的字符
////        btn_r2.setId(25);
////        btn_r2.setBackgroundColor(Color.YELLOW);
        LinearLayout.LayoutParams layoutParams_img = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams_img.gravity = Gravity.CENTER_VERTICAL;

            // TODO: 2021/6/12
            layoutParams_img.setMargins(10, 0, 100, 0);

//        ImageView mImageView = new ImageView(getApplicationContext());
//        mImageView.setBackgroundResource(R.drawable.img_close);

        Button mImageView = new Button(this);
//        mImageView.setBackgroundColor(Color.RED);

        relative.addView(mImageView);
//        relative.addView(btn_r2);
//        relative.addView(view2);

// 设置RelativeLayout布局的宽高
        lp=new RelativeLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        btn_r1.setLayoutParams(lp); ////设置按钮的布局属性


        lp=new RelativeLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, btn_r1.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, btn_r1.getId());


        mImageView.setLayoutParams(lp); ////设置按钮的布局属性
//        btn_r2.setLayoutParams(lp); ////设置按钮的布局属性
//        btn_r2.setOnClickListener(
//                new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        relative.setVisibility(View.GONE);
//                    }
//                }
//        );


        lp=new RelativeLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL, mImageView.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, mImageView.getId());

        view2.setLayoutParams(lp); ////设置布局属性

//        setContentView(relative);
//        viewById.addView(relative);
        rl.addView(relative);



//        TextView tvp = (TextView) findViewById(R.id.tv_p);
//        TextView textView = new TextView(this);
//        textView.setText("hello");
//        textView.setTextSize(20);
//        textView.setGravity(Gravity.CENTER);
//        textView.setBackgroundColor(Color.GREEN);
//        textView.setVisibility(View.VISIBLE);
//        rl.addView(textView,200,200);
//        viewById.addView(textView,200,200);


//        rl.addView(view2);

    }

    public void click(View view) {
        Toast.makeText(this,"tusi",Toast.LENGTH_SHORT).show();
    }
}